
package com.jf.cloud.user.service.impl;

import com.jf.cloud.api.user.dto.FinanceDetailDTO;
import com.jf.cloud.api.user.vo.FinanceDetailExcelVO;
import com.jf.cloud.api.user.vo.FinanceDetailVO;
import com.jf.cloud.api.user.vo.UserPayInfoVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.user.constant.LevelTypeEnum;
import com.jf.cloud.user.dto.UserLevelLogDTO;
import com.jf.cloud.user.mapper.UserLevelLogMapper;
import com.jf.cloud.user.model.UserLevelLog;
import com.jf.cloud.user.service.UserLevelLogService;
import com.jf.cloud.user.vo.UserLevelLogVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户等级记录
 *
 * @author zz
 * @date 2021-05-14 11:04:52
 */
@Service
public class UserLevelLogServiceImpl implements UserLevelLogService {

    @Autowired
    private UserLevelLogMapper userLevelLogMapper;
    @Autowired
    private MapperFacade mapperFacade;

    @Override
    public PageVO<UserLevelLogVO> pageBuyLevelLog(PageDTO pageDTO, UserLevelLogDTO userLevelLogDTO) {
        return PageUtil.doPage(pageDTO, () -> userLevelLogMapper.pageBuyLevelLog(userLevelLogDTO));
    }

    @Override
    public UserLevelLog getByLevelLogId(Long levelLogId) {
        return userLevelLogMapper.getByLevelLogId(levelLogId);
    }

    @Override
    public void save(UserLevelLog userLevelLog) {
        userLevelLogMapper.save(userLevelLog);
    }

    @Override
    public void update(UserLevelLog userLevelLog) {
        userLevelLogMapper.update(userLevelLog);
    }

    @Override
    public void deleteById(Long levelLogId) {
        userLevelLogMapper.deleteById(levelLogId);
    }

    @Override
    public int batchSaveUserLevelLogs(List<UserLevelLog> userLevelLogs) {
        return userLevelLogMapper.batchSaveUserLevelLogs(userLevelLogs);
    }

    @Override
    public Integer getMaxLevelByUserId(Long userId) {
        return userLevelLogMapper.getMaxLevelByUserId(userId);
    }

    @Override
    public int updateToSuccess(Long levelLogId, Long payId, Integer payType) {
        return userLevelLogMapper.updateToSuccess(levelLogId, payId, payType);
    }

    @Override
    public void deleteByUserId(Long userId) {
        userLevelLogMapper.deleteByUserId(userId);
    }

    @Override
    public Integer getUserTypeAtTime(Long userId, Date createTime) {
        Integer userType = userLevelLogMapper.getUserTypeAtTime(userId, createTime);
        if (Objects.isNull(userType)) {
            userType = LevelTypeEnum.ORDINARY_USER.value();
        }
        return userType;
    }

    @Override
    public PageVO<FinanceDetailVO> pageFinanceDetail(PageDTO pageDTO, FinanceDetailDTO financeDetailDTO) {
        return PageUtil.doPage(pageDTO, ()-> userLevelLogMapper.listFinanceDetail(financeDetailDTO));
    }

    @Override
    public List<FinanceDetailExcelVO> listFinanceDetail(FinanceDetailDTO financeDetailDTO) {
        return mapperFacade.mapAsList(userLevelLogMapper.listFinanceDetail(financeDetailDTO), FinanceDetailExcelVO.class);
    }

    @Override
    public List<UserPayInfoVO> listPayInfoVO(Date startTime, Date endTime) {
        return userLevelLogMapper.listPayInfo(startTime, endTime);
    }


}
